<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-s-home"></i>后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName">
      <el-table-column prop="id" label="ID" align="center" width="50"></el-table-column>
      <el-table-column prop="username" label="用户名" align="center" width="60"></el-table-column>
      <el-table-column prop="password" label="密码" align="center" width="180"></el-table-column>
      <el-table-column prop="nickname" label="昵称" align="center" width="100"></el-table-column>
      <el-table-column prop="avatar" label="头像url" align="center" width="180"></el-table-column>
      <el-table-column prop="phone" label="电话" align="center" width="60"></el-table-column>
      <el-table-column prop="email" label="电子邮箱" align="center" width="60"></el-table-column>
      <el-table-column prop="description" label="描述" align="center" width="140"></el-table-column>
      <el-table-column prop="lastLoginIp" label="上次登录ip" align="center" width="60"></el-table-column>
      <el-table-column prop="loginCount" label="登录次数" align="center" width="60"></el-table-column>
      <el-table-column prop="gmtLastLogin" label="上传登录时间" align="center" width="60"></el-table-column>
      <el-table-column prop="gmtCreate" label="创建时间" align="center" width="100"></el-table-column>
      <el-table-column prop="gmtModified" label="修改时间" align="center" width="100"></el-table-column>
      <el-table-column prop="enable" label="是否启用" >
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.enable"
              @change="changeEnable(scope.row)"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="grey">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" >
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" circle
            @click="handleEdit(scope.row)" ></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" circle
            @click="openHandleDelete(scope.row)" ></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  name: "AlbumListView",
  data() {
    return {
      tableData: []
    }
  },
  created(){
    console.log("created...")
  },
  mounted() {
    console.log("mounted...")
    this.loadAlbumList()
  },
  methods: {
    changeEnable(admin){
      let enableText = ['禁用','启用']
      console.log("修改账号状态")
      console.log(admin)
      let url = "http://localhost:9081/admins/" + admin.id
      url += admin.enable == 1 ? "/enable" : "/disable"
      /*if(admin.enable == 1){
        console.log("启用账号")
        url += "/enable"
      }else {
        console.log("禁用账号")
        url += "/disable"
      }*/
        this.axios
            .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
            .post(url).then(response=>{
          let responseBody = response.data
          console.log('responseBody参数：')
          console.log(responseBody)
          if(responseBody.state == 20000){
            this.$message.success(enableText[admin.enable] + admin.username + "账号成功")
          }else {
            // this.$message.error(responseBody.message)
            let title = '操作失败'
            this.$alert(responseBody.message, title, {
              confirmButtonText: '确定',
              callback: action => {
                this.loadAlbumList()
              }
            });
          }
        })
    },
    tableRowClassName({row, rowIndex}) {
      if (rowIndex === 1) {
        return 'warning-row';
      } else if (rowIndex === 3) {
        return 'success-row';
      }
      return '';
    },
    loadAlbumList(){
      console.log("loadAlbumList加载中")
      let url = "http://localhost:9081/admins"
      // 会有预检的行为，是浏览器的行为，如果有缓冲，浏览器只会发送预检请求一次
      this.axios
          .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .get(url).then(response =>{
        console.log("response参数：")
        console.log(response)
        let responseBody = response.data;
        console.log("response的data参数：")
        console.log(responseBody)
        this.tableData = responseBody.data
        console.log("loadAlbumList加载完毕")
      })
    },
    openHandleDelete(admin) {
      this.$confirm('是否删除【'+admin.username+'(id='+admin.id+')】', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
          this.handleDelete(admin)
      }).catch(() => {
      });
    },
    handleDelete(admin){
      // alert("你点击了【"+name+"(id="+id+")】的删除按钮")
      let url = 'http://localhost:9081/admins/'+admin.id+'/delete'
      this.axios
          .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .post(url).then(response=>{
        let responseBody = response.data
        if(responseBody.state != 20000){
          this.$message.error(responseBody.message)
        }
        this.loadAlbumList()
      })
    },
    handleEdit(admin){
      alert("你点击了【"+admin.username+"(id="+admin.id+")】的编辑按钮")
    }
  }
}
</script>

<style scoped>

</style>